<?php

/**
 * This file is part of Switcher.
 * 
 * Switcher is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Switcher is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Switcher. If not, see <http://www.gnu.org/licenses/>.
 * 
 * @license http://www.opensource.org/licenses/gpl-3.0.html GPL-3.0
 */

namespace mustache;

/**
 * This class represents a section in the template. It manages context-related operations.
 * 
 * @author Dri <switcher.framework@gmail.com>
 * @license http://www.opensource.org/licenses/gpl-3.0.html GPL-3.0
 * @since 1.0.0
 */
final class Section
{
	/**
	 * The position of the section in the template.
	 * @var int
	 */
	public $position = 0;
	
	/**
	 * Indicates if the section has to be rendered.
	 * @var bool
	 */
	public $rendered = true;
	
	/**
	 * The name of the section.
	 * @var string
	 */
	public $name = null;
	
	/**
	 * The list of contexts for the section
	 * @var array
	 */
	private $contexts = array();
	
	/**
	 * Hidden constructor.
	 */
	private function __construct()
	{
	}
	
	/**
	 * Returns the current context.
	 * @return mixed The current context
	 */
	public function context()
	{
		return $this->contexts[0];
	}
	
	/**
	 * Removes the current context.
	 */
	public function popContext()
	{
		array_shift($this->contexts);
	}
	
	/**
	 * Returns the number of remaining contexts.
	 * @return int The number of remaining contexts.
	 */
	public function size()
	{
		return count($this->contexts);
	}
	
	/**
	 * Indicates whether a variable exists in the context, without interpolation.
	 * @param string $name The base name of the variable
	 * @return bool <code>true</code> if the variable exists
	 */
	public function hasVariable($name)
	{
		$context = $this->context();
		
		if ( is_object($context) && isset($context->$name) )
		{
			return true;
		}
		
		return is_array($context) && array_key_exists($name, $context);
	}
	
	/**
	 * Resolves a variable in the context from dot-separated names.
	 * @param array $parts A list of the dot-separated names
	 * @return mixed The value or an empty string
	 */
	public function interpolateVariable(array $parts)
	{
		$value = $this->context();
		
		foreach ($parts as $part)
		{
			if ( is_object($value) && isset($value->$part) )
			{
				$value = $value->$part;
			}
			elseif ( is_array($value) && array_key_exists($part, $value) )
			{
				$value = $value[$part];
			}
			else
			{
				return '';
			}
		}
		
		return $value;
	}
	
	/**
	 * Creates the root section of the template.
	 * 
	 * @param mixed $context The initial context
	 * @return Section The root section
	 */
	public static function newRootSection($context)
	{
		$section = new Section();
		$section->contexts = array($context);
		
		return $section;
	}
	
	/**
	 * Creates a nested section.
	 * 
	 * @param object $tag The opening section tag.
	 * @param bool $rendered Indicates if the section is rendered
	 * @param array $contexts The list of contexts
	 */
	public static function newNestedSection($tag, $rendered, array $contexts)
	{
		$section = new Section();
		
		$section->rendered = $rendered;
		$section->position = $tag->position + $tag->length;
		$section->name = $tag->content;
		$section->contexts = $contexts;
		
		return $section;
	}
}

?>